﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleSocket
{

    /// <summary>
    /// This class is used for building the message that gets sent through the socket.
    /// </summary>
    internal class MessageBuilder
    {
        public bool RequestResponce { get; set; }

        public bool ContainsMessage { get; set; }
        private byte[] _message;
        public byte[] Message
        {
            get
            {
                return _message;
            }
            set
            {
                ContainsMessage = true;
                _message = value;
            }
        }

        public bool ContainsCommand { get; set; }
        private int _command;
        public int Command
        {
            get
            {
                return _command;
            }
            set
            {
                ContainsCommand = true;
                _command = value;
            }
        }

        public bool ContainsCommandText { get; set; }
        private string _commandText;
        public string CommandText
        {
            get
            {
                return _commandText;
            }
            set
            {
                ContainsCommandText = true;
                _commandText = value;
            }
        }

        public bool ContainsResponceID { get; set; }
        private uint _responceID;
        public uint ResponceID
        {
            get
            {
                return _responceID;
            }
            set
            {
                ContainsResponceID = true;
                _responceID = value;
            }
        }

        /// <summary>
        /// Creates the byte[] that gets sent through the socket.
        /// </summary>
        /// <returns></returns>
        public byte[] GetMessage()
        {

            if (RequestResponce && ContainsResponceID == false)
                throw new Exception("ResponceID must be supplied when requesting a responce");

            List<byte[]> dataList = new List<byte[]>();

            MessageFlags flags = new MessageFlags();

            if (ContainsResponceID && RequestResponce)
            {
                flags |= MessageFlags.ResponceRequest;
            }

            if (ContainsResponceID && !RequestResponce)
            {
                flags |= MessageFlags.Responce;
            }

            if (ContainsResponceID)
            {
                dataList.Add(BitConverter.GetBytes(ResponceID));
            }

            if (ContainsCommand)
            {
                flags |= MessageFlags.Command;
                dataList.Add(BitConverter.GetBytes(Command));
            }

            if (ContainsCommandText)
            {
                flags |= MessageFlags.CommandText;
                byte[] commandTextData = Encoding.UTF8.GetBytes(CommandText ?? "");
                byte[] commandTextLen = BitConverter.GetBytes(commandTextData.Length);
                dataList.Add(commandTextLen);
                dataList.Add(commandTextData);
            }

            if (ContainsMessage)
            {
                flags |= MessageFlags.DataPackage;
                dataList.Add(BitConverter.GetBytes(Message.Length));
                dataList.Add(Message);
            }

            byte[] flagsData = BitConverter.GetBytes((int)flags);
            dataList.Insert(0, flagsData);

            return Helpers.Combine(dataList.ToArray());
        }

    }
}
